package com.haohan.jingyuan.customer.controller;

import cn.hutool.json.JSONUtil;
import com.haohan.jingyuan.customer.dto.req.CreateFileAuthRequest;
import com.haohan.jingyuan.customer.dto.req.CreateFileShareRequest;
import com.haohan.jingyuan.customer.dto.req.DeleteFileAuthRequest;
import com.haohan.jingyuan.customer.dto.req.DeleteFileShareRequest;
import com.haohan.jingyuan.customer.dto.req.QueryFileAuthListRequest;
import com.haohan.jingyuan.customer.dto.req.QueryFileShareListRequest;
import com.haohan.jingyuan.customer.dto.req.UpdateFileAuthRequest;
import com.haohan.jingyuan.customer.dto.req.UpdateFileShareRequest;
import com.haohan.jingyuan.customer.dto.resp.FileAuthInfoResp;
import com.haohan.jingyuan.customer.dto.resp.FileAuthListResp;
import com.haohan.jingyuan.customer.dto.resp.FileShareInfoResp;
import com.haohan.jingyuan.customer.dto.resp.FileShareListResp;
import com.haohan.jingyuan.customer.service.CustomerFileAuthService;
import com.haohan.jingyuan.customer.service.CustomerFileShareService;
import com.haohan.jingyuan.gateway.aspect.NeedLogin;
import com.haohan.jingyuan.gateway.dto.GwRequest;
import com.haohan.jingyuan.gateway.dto.GwResponse;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import javax.validation.Valid;

/**
 * 客户文件分享控制器
 *
 * @ClassName CustomerFileShareController
 * @Description 客户文件分享相关接口控制器
 * @Author hexijun
 * @Time 2025/11/04 17:00
 * @Version 1.0
 */
@Slf4j
@RestController
@RequiredArgsConstructor
public class CustomerFileShareController {

    /**
     * 客户文件分享服务
     */
    private final CustomerFileShareService customerFileShareService;

    /**
     * 客户文件授权服务
     */
    private final CustomerFileAuthService customerFileAuthService;

    /**
     * 新增分享
     *
     * @param request 新增分享请求
     * @return 分享信息
     */
    @PostMapping("/customer/fileshare/create")
    @NeedLogin
    public GwResponse<FileShareInfoResp> createFileShare(@Valid @RequestBody GwRequest<CreateFileShareRequest> request) {
        log.info("CustomerFileShareController.createFileShare BEGIN ==> {}", JSONUtil.toJsonStr(request));

        FileShareInfoResp fileShareInfo = customerFileShareService.createFileShare(request.getRequestData());
        GwResponse<FileShareInfoResp> response = GwResponse.success("新增分享成功", fileShareInfo);

        log.info("CustomerFileShareController.createFileShare END ==> {}", JSONUtil.toJsonStr(response));
        return response;
    }

    /**
     * 编辑分享
     *
     * @param request 编辑分享请求
     * @return 分享信息
     */
    @PostMapping("/customer/fileshare/update")
    @NeedLogin
    public GwResponse<FileShareInfoResp> updateFileShare(@Valid @RequestBody GwRequest<UpdateFileShareRequest> request) {
        log.info("CustomerFileShareController.updateFileShare BEGIN ==> {}", JSONUtil.toJsonStr(request));

        FileShareInfoResp fileShareInfo = customerFileShareService.updateFileShare(request.getRequestData());
        GwResponse<FileShareInfoResp> response = GwResponse.success("编辑分享成功", fileShareInfo);

        log.info("CustomerFileShareController.updateFileShare END ==> {}", JSONUtil.toJsonStr(response));
        return response;
    }

    /**
     * 删除分享
     *
     * @param request 删除分享请求
     * @return 删除结果
     */
    @PostMapping("/customer/fileshare/delete")
    @NeedLogin
    public GwResponse<String> deleteFileShare(@Valid @RequestBody GwRequest<DeleteFileShareRequest> request) {
        log.info("CustomerFileShareController.deleteFileShare BEGIN ==> {}", JSONUtil.toJsonStr(request));

        boolean success = customerFileShareService.deleteFileShare(request.getRequestData());
        GwResponse<String> response = null;
        if (success) {
            response = GwResponse.success("删除分享成功", null);
        } else {
            response = GwResponse.error("删除分享失败");
        }

        log.info("CustomerFileShareController.deleteFileShare END ==> {}", JSONUtil.toJsonStr(response));
        return response;
    }

    /**
     * 分页查询分享列表
     *
     * @param request 查询分享列表请求
     * @return 分享列表
     */
    @PostMapping("/customer/fileshare/list")
    @NeedLogin
    public GwResponse<FileShareListResp> queryFileShareList(@Valid @RequestBody GwRequest<QueryFileShareListRequest> request) {
        log.info("CustomerFileShareController.queryFileShareList BEGIN ==> {}", JSONUtil.toJsonStr(request));

        FileShareListResp fileShareList = customerFileShareService.queryFileShareList(request.getRequestData());
        GwResponse<FileShareListResp> response = GwResponse.success("查询分享列表成功", fileShareList);

        log.info("CustomerFileShareController.queryFileShareList END ==> {}", JSONUtil.toJsonStr(response));
        return response;
    }

    /**
     * 新增授权
     *
     * @param request 新增授权请求
     * @return 授权信息
     */
    @PostMapping("/customer/fileshare/auth/create")
    @NeedLogin
    public GwResponse<FileAuthInfoResp> createFileAuth(@Valid @RequestBody GwRequest<CreateFileAuthRequest> request) {
        log.info("CustomerFileShareController.createFileAuth BEGIN ==> {}", JSONUtil.toJsonStr(request));

        FileAuthInfoResp fileAuthInfo = customerFileAuthService.createFileAuth(request.getRequestData());
        GwResponse<FileAuthInfoResp> response = GwResponse.success("新增授权成功", fileAuthInfo);

        log.info("CustomerFileShareController.createFileAuth END ==> {}", JSONUtil.toJsonStr(response));
        return response;
    }

    /**
     * 编辑授权
     *
     * @param request 编辑授权请求
     * @return 授权信息
     */
    @PostMapping("/customer/fileshare/auth/update")
    @NeedLogin
    public GwResponse<FileAuthInfoResp> updateFileAuth(@Valid @RequestBody GwRequest<UpdateFileAuthRequest> request) {
        log.info("CustomerFileShareController.updateFileAuth BEGIN ==> {}", JSONUtil.toJsonStr(request));

        FileAuthInfoResp fileAuthInfo = customerFileAuthService.updateFileAuth(request.getRequestData());
        GwResponse<FileAuthInfoResp> response = GwResponse.success("编辑授权成功", fileAuthInfo);

        log.info("CustomerFileShareController.updateFileAuth END ==> {}", JSONUtil.toJsonStr(response));
        return response;
    }

    /**
     * 删除授权
     *
     * @param request 删除授权请求
     * @return 删除结果
     */
    @PostMapping("/customer/fileshare/auth/delete")
    @NeedLogin
    public GwResponse<String> deleteFileAuth(@Valid @RequestBody GwRequest<DeleteFileAuthRequest> request) {
        log.info("CustomerFileShareController.deleteFileAuth BEGIN ==> {}", JSONUtil.toJsonStr(request));

        boolean success = customerFileAuthService.deleteFileAuth(request.getRequestData());
        GwResponse<String> response = null;
        if (success) {
            response = GwResponse.success("删除授权成功", null);
        } else {
            response = GwResponse.error("删除授权失败");
        }

        log.info("CustomerFileShareController.deleteFileAuth END ==> {}", JSONUtil.toJsonStr(response));
        return response;
    }

    /**
     * 查询授权列表
     *
     * @param request 查询授权列表请求
     * @return 授权列表
     */
    @PostMapping("/customer/fileshare/auth/list")
    @NeedLogin
    public GwResponse<FileAuthListResp> queryFileAuthList(@Valid @RequestBody GwRequest<QueryFileAuthListRequest> request) {
        log.info("CustomerFileShareController.queryFileAuthList BEGIN ==> {}", JSONUtil.toJsonStr(request));

        FileAuthListResp fileAuthList = customerFileAuthService.queryFileAuthList(request.getRequestData());
        GwResponse<FileAuthListResp> response = GwResponse.success("查询授权列表成功", fileAuthList);

        log.info("CustomerFileShareController.queryFileAuthList END ==> {}", JSONUtil.toJsonStr(response));
        return response;
    }
}

